package com.qf.dao.impl;

import com.qf.dao.GoodsTypeDao;
import com.qf.pojo.TbGoodsType;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/16 16:52
 */
public class GoodsTypeDaoImpl implements GoodsTypeDao {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public List<TbGoodsType> selectAll(int level) {
        String sql="select * from tb_goods_type where level=? and status=1";
        try {
            return queryRunner.query(sql, new BeanListHandler<>(TbGoodsType.class),level);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public TbGoodsType selectById(long typeid) {

        String sql="select * from tb_goods_type where id=?";
        try {
            return queryRunner.query(sql, new BeanHandler<>(TbGoodsType.class),typeid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<TbGoodsType> selectAllGt() {
        String sql="select * from tb_goods_type";
        try {
            return queryRunner.query(sql, new BeanListHandler<>(TbGoodsType.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insertGoodsType(String name) {
        String sql="insert into tb_goods_type(name ,level,Parent,status) values(?,1,0,1) ";
        try {
             queryRunner.update(sql,name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
